
class Board {

    def n
    def count

    Board(n) {
        this.n = n
        count = 1
        solve()
    }

    def solve(row = 0, columns = [], slashes = [] as Set, backSlashes = [] as Set)
    {
        // Condicion de corte
        if (row >= n) return true

        def slash     = { row + it }
        def backSlash = { n - 1 + row - it }
        def valido    = { !columns.contains(it) &&
                          !slashes.contains(slash(it)) &&
                          !backSlashes.contains(backSlash(it))}

        def list = (0..<n).findAll ( valido )

        // Se puede iterar sobre objetos nulos!
        list.each {
            if (solve(row + 1, columns + it,
                    slashes + slash(it),
                    backSlashes + backSlash(it))) {

                printBoard( columns + it )
            }
        }
        false
    }

    def printBoard(columns)
    {
        def line = ""
        (0..n*3).each { line += "-" }
        println count++
        println(line)
        columns.each {
            (0..<it).each {print("|  ") }
            print("|QQ")
            (it+1..<n).each { print("|  ") }
            println("|")
        }
        println(line)
    }
}

// Resolver tablero
b = new  Board(8)
